package com.qkjia.day01;

public class Test07_Commulication {
    public static void main(String[] args) {
        Business business = new Business();
        new Thread(()->{
            try {
                for (int i = 0; i <30 ; i++) {
                    business.incr();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        },"父亲1").start();
        new Thread(()->{
            try {
                for (int i = 0; i <30 ; i++) {
                    business.decr();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"儿子1").start();
        //---------------------------------------


        new Thread(()->{
            try {
                for (int i = 0; i <30 ; i++) {
                    business.incr();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        },"父亲2").start();
        new Thread(()->{
            try {
                for (int i = 0; i <30 ; i++) {
                    business.decr();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"儿子2").start();
    }

}
class Business{

    public int num=0;
    public synchronized void incr() throws InterruptedException {
        //判断
        while(num!=0){
            //如果不是0 说明不能再加了
            this.wait();
        }
        //干活
        System.out.println(Thread.currentThread().getName()+"\t"+num);
        num++;
        //通知
        this.notifyAll();
    }
    public synchronized void decr() throws InterruptedException {
        //判断
        while(num==0){
            this.wait();
        }
        //干活
        System.out.println(Thread.currentThread().getName()+"\t"+num);
        num--;
        //通知
        this.notifyAll();
    }
}
